/*
  A *very* quick and dirty debug console. Basically just a floating div that gives you an easy place
  to output a bunch of text for debugging purposes.

  Author: Mike Thomas
  Date: 1-5-08
*/

var DEBUG_DIV_ID = 'debug';

var HANDLE_SIZE = 5;
var DRAGMODE_MOVE = 0;
var DRAGMODE_RESIZE_N = 1;
var DRAGMODE_RESIZE_S = 2;
var DRAGMODE_RESIZE_E = 3;
var DRAGMODE_RESIZE_W = 4;
var DRAGMODE_RESIZE_NE = 5;
var DRAGMODE_RESIZE_NW = 6;
var DRAGMODE_RESIZE_SE = 7;
var DRAGMODE_RESIZE_SW = 8;

function Debug() {
  this.div = null;
  this.created = false;
  this.dragMode = -1;
  this.dragging = false;
  this.x = 0;
  this.y = 0;
  this.w = 300;
  this.h = 100;
};

Debug.prototype.createConsole = function() {
  if (this.created) return;
  
  this.div = document.createElement('DIV');
  this.div.id = DEBUG_DIV_ID;
  this.div.style.position = 'absolute';
  this.div.style.left = this.x + 'px';
  this.div.style.top = this.y + 'px';
  this.div.style.width = this.w + 'px';
  this.div.style.height = this.h + 'px';
  this.div.style.backgroundColor = '#ffffff';
  this.div.style.fontSize = '10px';
  this.div.style.color = '#000000';
  this.div.style.overflow = 'auto';
  this.div.style.padding = '5px';
  this.div.style.border = '1px solid #000';
  
  var div = this.div;
  var self = this;
  div.onmousemove = function(e) {
    var mousePosition = util.getMousePosition(e);
    var x = mousePosition[0] - self.x;
    var y = mousePosition[1] - self.y;
    var handleX = self.w - HANDLE_SIZE;
    var handleY = self.h - HANDLE_SIZE;
    if (x < HANDLE_SIZE) {
      if (y < HANDLE_SIZE) {
        self.setDragMode(DRAGMODE_RESIZE_NW);
        div.style.cursor = 'nw-resize';
      } else if (y > handleY) {
        self.setDragMode(DRAGMODE_RESIZE_SW);
        div.style.cursor = 'sw-resize';
      } else {
        self.setDragMode(DRAGMODE_RESIZE_W);
        div.style.cursor = 'w-resize';
      }
    } else if (x > handleX) {
      if (y < HANDLE_SIZE) {
        self.setDragMode(DRAGMODE_RESIZE_NE);
        div.style.cursor = 'ne-resize';
      } else if (y > handleY) {
        self.setDragMode(DRAGMODE_RESIZE_SE);
        div.style.cursor = 'se-resize';
      } else {
        self.setDragMode(DRAGMODE_RESIZE_E);
        div.style.cursor = 'e-resize';
      }
    } else {
      if (y < HANDLE_SIZE) {
        self.setDragMode(DRAGMODE_RESIZE_N);
        div.style.cursor = 'n-resize';
      } else if (y > handleY) {
        self.setDragMode(DRAGMODE_RESIZE_S);
        div.style.cursor = 's-resize';
      } else {
        self.setDragMode(DRAGMODE_MOVE);
        div.style.cursor = 'move';
      }
    }
  };
  div.onmousedown = function(e) {
    var mousePosition = util.getMousePosition(e);
    var x = mousePosition[0];
    var y = mousePosition[1];
    self.startDragging();
    self.clickX = x;
    self.clickY = y;
  };
  div.ondblclick = function(e) {
    self.close();
  };
  document.getElementsByTagName('body')[0].appendChild(this.div);
  document.onmousemove = function(e) {
    var mousePosition = util.getMousePosition(e);
    var x = mousePosition[0];
    var y = mousePosition[1];
    debug.mouseMove(x, y);
  };
  document.onmouseup = function(e) {
    debug.mouseUp(e);
  };
  this.created = true;
};

Debug.prototype.print = function(string) {
  this.createConsole();
  $(DEBUG_DIV_ID).innerHTML += '<br>' + string;
}

Debug.prototype.clear = function() {
  if (this.created) {
    $(DEBUG_DIV_ID).innerHTML = '';
  }
};

Debug.prototype.close = function() {
  if (this.created) {
    this.created = false;
    this.div.parentNode.removeChild(this.div);
    delete this.div;
  }
};

Debug.prototype.setText = function(string) {
  this.createConsole();
  $(DEBUG_DIV_ID).innerHTML = string;
};

Debug.prototype.setDragMode = function(dragMode) {
  if (!this.dragging) {
    this.dragMode = dragMode;
  }
}
Debug.prototype.mouseMove = function(x, y) {
  if (this.dragging == true) {
    var deltaX = x - this.clickX;
    var deltaY = y - this.clickY;
    var newX, newY, newW, newH;
    if (this.dragMode == DRAGMODE_MOVE) {
      this.incrementX(deltaX);
      this.incrementY(deltaY);
    } else if (this.dragMode == DRAGMODE_RESIZE_N) {
      this.incrementY(deltaY);
      this.incrementH(-1 * deltaY);
    } else if (this.dragMode == DRAGMODE_RESIZE_S) {
      this.incrementH(deltaY);
    } else if (this.dragMode == DRAGMODE_RESIZE_W) {
      this.incrementX(deltaX);
      this.incrementW(-1 * deltaX);
    } else if (this.dragMode == DRAGMODE_RESIZE_E) {
      this.incrementW(deltaX);
    } else if (this.dragMode == DRAGMODE_RESIZE_NE) {
      this.incrementY(deltaY);
      this.incrementW(deltaX);
      this.incrementH(-1 * deltaY);
    } else if (this.dragMode == DRAGMODE_RESIZE_NW) {
      this.incrementX(deltaX);
      this.incrementY(deltaY);
      this.incrementW(-1 * deltaX);
      this.incrementH(-1 * deltaY);
    } else if (this.dragMode == DRAGMODE_RESIZE_SE) {
      this.incrementW(deltaX);
      this.incrementH(deltaY);
    } else if (this.dragMode == DRAGMODE_RESIZE_SW) {
      this.incrementX(deltaX);
      this.incrementW(-1 * deltaX);
      this.incrementH(deltaY);
    }
    this.refresh();
    this.clickX = x;
    this.clickY = y;
    return false;
  }
}
Debug.prototype.mouseUp = function(e) {
  this.dragging = false;
}
Debug.prototype.incrementX = function(increment) {
  this.x += increment;
}
Debug.prototype.incrementY = function(increment) {
  this.y += increment;
}
Debug.prototype.incrementW = function(increment) {
  this.w += increment;
}
Debug.prototype.incrementH = function(increment) {
  this.h += increment;
}
Debug.prototype.startDragging = function() {
  this.dragging = true;
  document.body.style.MozUserSelect = 'none';
}
Debug.prototype.refresh = function() {
  this.div.style.left = this.x + 'px';
  this.div.style.top = this.y + 'px';
  this.div.style.width = this.w + 'px';
  this.div.style.height = this.h + 'px';
}
var debug = new Debug();
